<template>
  <d2-container>
    <div class="row-ac pb-2">
      <div class="cp"><el-button type="warning" @click="addDuties" icon="el-icon-plus">添加职务</el-button></div>
    </div>
    <div class="flex-1 ml-2">
      <el-table :data="dutiesList" border stripe>
        <el-table-column align="center" type="selection" width="55" />
        <el-table-column align="center" label="序号" type="index" width="65" />
        <el-table-column align="center" label="职务名称" prop="name" width="130" />
        <el-table-column align="center" label="操作" width="180">
          <template slot-scope="scope">
            <el-button size="mini" @click="dutiesEdit(scope.row.id)">修改</el-button>
            <el-button size="mini" type="danger" @click="openDelete(scope.row.id)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <el-dialog :visible.sync="dialogVisible" width="30%" title="添加/编辑职务">
      <el-form :model="form" :rules="rules" ref="form" label-width="80px">
        <el-form-item label="职务名称" prop="name">
          <div style="width: 50%;">
            <el-input v-model="form.name"></el-input>
          </div>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="clickCancel">取 消</el-button>
        <el-button type="primary" v-if="dialogSubmit" @click="submitForm">确 定</el-button>
        <el-button type="primary" v-if="dialogEdit" @click="handleEdit">保 存</el-button>
      </div>
    </el-dialog>
  </d2-container>
</template>

<script>
  import {
    dutiesUrl,
  } from '@/utils/interface';
  export default {
    data() {
      return {
        dialogVisible: false,
        dialogEdit: false,
        dialogSubmit: false,
        dutiesList: [],
        form: {
          name: ''
        },
        rules: {
          name: [{
            required: true,
            message: '请输入职务名称',
            trigger: 'blur'
          }]
        },
        takeDutiesId:''
      }
    },
    created() {
      this.initData()
    },
    methods: {
      initData() {
        this.getDuties()
      },
      getDuties() {
        this.$get(dutiesUrl).then(res => {
          this.dutiesList = res.data.data.list
        })
      },
      addDuties() {
        this.dialogVisible = true
        this.dialogEdit = false
        this.dialogSubmit = true
      },
      submitForm() {
        this.$refs.form.validate(valid => {
          if (valid) {
            this.$post(dutiesUrl, this.form).then(res => {
                this.getDuties()
                this.dialogVisible = false
                this.$message.success('添加成功')
                this.form = {}
                this.$refs.upload.clearFiles();
                this.$emit('refresh')
              })
          } else {
            return false
          }
        })
      },
      clickCancel() {
        this.dialogVisible = false
        this.form = {}
      },
      handleEdit() {
        this.$put(dutiesUrl + this.takeDutiesId + '/', this.form).then(res => {
          if (res.status == 200) {
            this.$message({
              type: 'success',
              message: '修改成功'
            });
            this.getDuties()
            this.form = {}
            this.dialogVisible = false
          }
        })
      },
      dutiesEdit(dutiesId) {
        this.dialogVisible = true
        this.dialogEdit = true
        this.dialogSubmit = false
        this.takeDutiesId = dutiesId
        this.$get(dutiesUrl + dutiesId + '/').then(res => {
          this.form = {
            name: res.data.data.name,
          }
        })
      },
      openDelete(dutiesId) {
        this.$confirm('此操作将永久删除该分类, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          // 在确定时执行handleDelete的删除
          this.handleDelete(dutiesId);
          this.$message({
            type: 'success',
            message: '删除成功!'
          });
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          });
        });
      },
      handleDelete(dutiesId){
        this.$del(dutiesUrl + dutiesId + '/').then(res => {
          this.getDuties()
        })
      }
    }
  }
</script>

<style>
</style>
